<?php

class BDynamicFormModel extends CFormModel {

    private $_dynamicData = array();
    private $_dynamicFields = array();
    private $safeAttributes = array();
    private $requiredAttributes = array();
    private $labels = array();

    protected function populateSafeAttributes($data = NULL) {
        $dynamicFields = array_keys($this->_dynamicFields);
        foreach ($dynamicFields as $name) {
            if (!in_array($name, $this->requiredAttributes))
                $this->safeAttributes[$name] = $name;
        }
        return $this->safeAttributes;
    }

    public function rules() {
       
//        $media_price_message_bookable='media_price_message_bookable';
//        array('media_price_message_bookable', '', 'on'=>'create,update');
//        // 测试
//        function checktagname($media_price_message_bookable,$params){
//        $oldtag = Tag::model()->findByAttributes(array('media_price_message_bookable'=>$this->tagname));
//        if($oldtag->tagid > 0){
//             $this->addError($attribute, '报错!');
//       }
//}
       // $this->addError('media_region_message_bookable', '报错');
        
        
        $safeString = '';
        $safeAttributes = $this->populateSafeAttributes();
        foreach ($safeAttributes as $name) {
            $safeString .= $name . ',';
        }
        $requireString = '';
        foreach ($this->requiredAttributes as $name) {
            $requireString .= $name . ',';
        }
        $rules = array();
        if ($safeString != '') {
            array_push($rules, array($safeString, 'safe'));
            
           // array_push($rules,array('media_price_message_bookable', 'required'));
        }
        if ($requireString != '') {
            array_push($rules, array($requireString, 'required'));
        }
        
        return $rules;

      //    return array($rules,array('firstname, lastname', 'safe'));
 
    }

    public function getSafeAttributeNames() {
        $list = parent::getSafeAttributeNames();
        $saftAttributes = $this->populateSafeAttributes();
        return $list + array_keys($saftAttributes);
    }

    public function attributeNames() {
        return array_merge(
                parent::attributeNames(), array_keys($this->_dynamicFields)
        );
    }

    /**
     * Returns the value for a dynamic attribute, if not, falls back to parent
     * method
     * 
     * @param type $name
     * @return type 
     */
    public function __get($name) {
        if (!empty($this->_dynamicFields[$name])) {
            if (!empty($this->_dynamicData[$name])) {
                return $this->_dynamicData[$name];
            } else {
                return null;
            }
        } else {
            return parent::__get($name);
        }
    }

    /**
     * Overrides the setter to store dynamic data.
     * 
     * @param type $name
     * @param type $val 
     */
    public function __set($name, $val) {
        if (!empty($this->_dynamicFields[$name])) {
            $this->_dynamicData[$name] = $val;
        } else {
            parent::__set($name, $val);
        }
    }

    public function addField($field, $defaultValue = NULL, $label = NULL) {
        $this->_dynamicFields[$field] = 1;
        $this->_dynamicData[$field] = $defaultValue;
        if ($label != NULL)
            $this->labels[$field] = $label;
    }

    public function attributeLabels() {
        $labels = parent::attributeLabels();
        $labels = $labels + $this->labels;
        return $labels;
    }

}

?>
